package com.zhongxi.guoyuan.view.seller;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.seller.SellerMallWithDrawAdapter;
import com.zhongxi.guoyuan.adapter.seller.SellerMallWithDrawResult;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.widget.view.MyScrollView;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SellerMallExchangeActivity extends BaseActivity {


    @Bind(R.id.ivBack)
    ImageView ivBack;
    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.mall_tv_total)
    TextView mallTvTotal;
    @Bind(R.id.mall_tv_noExchange)
    TextView mallTvNoExchange;
    @Bind(R.id.mall_et_num)
    EditText mallEtNum;
    @Bind(R.id.mall_tv_apply)
    TextView mallTvApply;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.tv_noData)
    TextView tvNoData;
    private String type;
    private String str;

    private List<SellerMallWithDrawResult.ListData> list;
    private LinearLayoutManager layoutManager;

    private SellerMallWithDrawAdapter adapter;
    private boolean isLoading;
    private int lastItem;
    private String trim;
    private String lastEarnings;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_seller_mall_exchange);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setText("申请提现");
    }

    @Override
    protected void initData() {
        super.initData();
        getWithDrawData();
        list = new ArrayList<>();
        layoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(layoutManager);
        adapter = new SellerMallWithDrawAdapter(this);
        recycleView.setAdapter(adapter);

    }

    private void getWithDrawData() {
        type = "list";
        str = "&page=" + pageNum;
        getServer(Url.BUSINESSCENTER_WITHDRAWLIST, null, str, true, null, "get");
    }

    private void postApplyWithDraw() {
        type = "apply";
        Map<String, String> map = new HashMap<>();
        map.put("amount", trim);
        getServer(Url.BUSINESSCENTER_INNERMALLWITHDRAW, map, "", true, null, "post");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastItem + 1 == adapter.getItemCount() && !isLoading) {
                    //滑动到底部加载更多
                    pageNum++;
                    getWithDrawData();
                    isLoading = true;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                lastItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

    @Override
    protected void processSuccessResult(String response) throws JSONException {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        if (type.equals("list")) {
            SellerMallWithDrawResult result = gson.fromJson(response, SellerMallWithDrawResult.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());

            if (pageNum == 1) {
                list.clear();
            }

            if (null != result && !"".equals(result)) {

                if (null != result.getEarningsNum() && !"".equals(result.getEarningsNum())) {
                    mallTvTotal.setText(result.getEarningsNum() + "美豆");
                } else {
                    mallTvTotal.setText("美豆");
                }

                if (null != result.getLastEarnings() && !"".equals(result.getLastEarnings())) {
                    lastEarnings = result.getLastEarnings();
                    mallTvNoExchange.setText(lastEarnings + "美豆");
                } else {
                    mallTvNoExchange.setText("美豆");
                }

                if (result.getList() != null && result.getList().size() > 0) {
                    list.addAll(result.getList());
                    adapter.setList(list);

                    tvNoData.setVisibility(View.GONE);
                } else {
                    if (pageNum==1){
                        tvNoData.setVisibility(View.VISIBLE);
                    }
                    if (pageNum > 1) {
                        pageNum--;
                    } else {
                        pageNum = 1;
                    }

                }

                adapter.notifyDataSetChanged();
            }

        } else if (type.equals("apply")) {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());
            showShortToast("申请成功，已进入受理状态");
            pageNum = 1;
            getWithDrawData();
        }

    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
        MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
        if (baseResponse.getCode() == 9002) {
            showShortToast("登录已失效，请重新登录");
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.ivBack, R.id.mall_tv_apply})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                onBackPressed();
                finish();
                break;
            case R.id.mall_tv_apply:
                trim = mallEtNum.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    showShortToast("请输入提现金额");
                } else if (!DataUtil.moreThanOther(trim, "100")) {
                    showShortToast("提现金额不能少于100");
                } else if (!DataUtil.moreThanOther(lastEarnings, trim)) {
                    showShortToast("超出您的未兑换余额");
                } else {
                    postApplyWithDraw();
                }
                break;
        }
    }


}
